
package com.monlyu.tag.extend;

import java.util.Map;

import com.google.common.collect.Maps;
import com.monlyu.bean.Pages;
import com.monlyu.tag.BaseTag;
import com.monlyu.util.FtlUtil;
import com.monlyu.util.LogUtil;

/********************************************************************************
 * <b>功能描述☞</b>分页中需要用到的标签<br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>monlyu<br>
 * <b>文件路径☞</b>com.monlyu.tag.extend.ImgATag.java<br>
 * <b>开发日期☞</b>2010-1-14 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version : 1.0.5
 ********************************************************************************/
public class PageTag extends BaseTag {

    private Object value;

    private boolean showSeaBar = false;

    public void setShowSeaBar(boolean showSeaBar){
        this.showSeaBar = showSeaBar;
    }

    public void setValue(Object value){
        this.value = value;
    }

    @Override
    public int doStartTag(){
        try {
            write2Page(FtlUtil.render2Ftl(toMap(), "page.ftl"));
        }
        catch (Exception e) {
            LogUtil.logWithDate(getClass(), "解析分页标签出错", e);
        }
        return SKIP_BODY;
    }

    private Map<String, Object> toMap(){
        Map<String, Object> data = Maps.newHashMap();
        Pages<?> pages = (Pages<?>) value;
        data.put("pageList", pages);
        data.put("map", pageContext.getRequest().getParameterMap());
        data.put("showBar", showSeaBar);
        data.put("current", pages == null ? 0 : pages.getCurrent());
        data.put("pageCount", pages == null ? 0 : pages.getPageCount());
        data.put("pageSize", pages == null ? 0 : pages.getSize());
        data.put("totalCount", pages == null ? 0 : pages.getTotalCount());
        return data;
    }
}
